/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.core.http;

import com.devoxy.roboeye.core.FileHelper;
import org.apache.http.entity.BasicHttpEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 21.05.11
 * Time: 16:44
 */
public class NewocrHttpEntity extends BasicHttpEntity {

    public static final String BOUNDARY = "7777777777777777777777777777777777";
    public static final String EOF = "\r\n";
    public static final String DELIMITER = "--" + BOUNDARY + EOF;

    private static final int OTHER_STRINGS_SIZE = 346;

    private static final Charset DEFAULT_CHARSET = Charset.forName("US-ASCII");

    private long bytesWritten;
    private String path;
    private String language;
    private String rotation;

    public NewocrHttpEntity(String path, String language, String rotation) {
        super();
        this.path = path;
        this.language = language;
        this.rotation = rotation;

        // size of http-response package consists of length of attached file, length of parameters (language and rotation)
        // and length of other strings that contains in http-header
        File f = new File(path);
        bytesWritten = f.length() + rotation.length() + getFileNameFromPath(path).length() + OTHER_STRINGS_SIZE;
    }

    @Override
    public long getContentLength() {
        return bytesWritten;
    }

    @Override
    public void writeTo(OutputStream os) throws IOException {
        StringBuilder sb = new StringBuilder();
        sb.append(DELIMITER);
        sb.append("Content-Disposition: form-data; name=\"userfile\"; filename=\"").append(getFileNameFromPath(path)).append("\"").append(EOF);
        sb.append("Content-Type: application/octet").append(EOF).append(EOF);
        /*bytesWritten +=*/ write(os, sb.toString());

        FileHelper.copyStreamContentAndCloseInput(new FileInputStream(path), os);

        sb = new StringBuilder(EOF);

        sb.append(DELIMITER);
        sb.append("Content-Disposition: form-data; name=\"l\"").append(EOF).append(EOF);
        sb.append(language).append(EOF);

        sb.append(DELIMITER);
        sb.append("Content-Disposition: form-data; name=\"r\"").append(EOF).append(EOF);
        sb.append(rotation).append(EOF);

        sb.append("--").append(BOUNDARY).append("--");
        /*bytesWritten += */ write(os, sb.toString());

        os.flush();
        os.close();
    }

    private int write(OutputStream os, String string) throws IOException {
        ByteBuffer encoded = DEFAULT_CHARSET.encode(CharBuffer.wrap(string));
        byte[] bytes = encoded.array();
        os.write(bytes, 0, bytes.length);
        return bytes.length;
    }

    private String getFileNameFromPath(String path) {
        String[] parts = path.split("/");
        return parts[parts.length - 1];
    }
}
